/**
 * 
 */
package com.jolestar.yayaweibotong.task;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import org.apache.commons.lang.StringUtils;

import com.google.apphosting.api.DeadlineExceededException;
import com.jolestar.yayaweibotong.ApiReachLimitException;
import com.jolestar.yayaweibotong.NetworkException;
import com.jolestar.yayaweibotong.YaYaConstants;
import com.jolestar.yayaweibotong.YayaException;
import com.jolestar.yayaweibotong.dao.AccountDomain;
import com.jolestar.yayaweibotong.dao.StatusDomain;
import com.jolestar.yayaweibotong.dao.UserDomain;
import com.jolestar.yayaweibotong.service.UserService;

/**
 * @author jolestar@gmail.com
 * 
 */
public class GetStatusTask extends AbstractTask {

	public static final String QUENE_NAME = "get-status-queue";

	/**
	 * @param name
	 */
	public GetStatusTask() {
		super(QUENE_NAME);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jolestar.yayaweibotong.task.AbstractTask#doTask(java.util.Map)
	 */
	@Override
	protected boolean doTask(Map<String, String> headers,Map<String, String> params) {
		String accountKeyName = params.get("account");
		if (StringUtils.isBlank(accountKeyName)) {
			return true;
		}
		AccountDomain account = UserService.ServiceHolder.getInstance()
				.getAccount(accountKeyName);
		if (account == null) {
			log.warning("can not find AccountDomain with keyName:"
					+ accountKeyName);
			return true;
		}
		UserDomain user = account.getUser();
		List<StatusDomain> statuses = null;
		try {
			statuses = account.getServiceType().getStatusService()
					.getLatestStatuses(account, account.getLastSyncStatusId());
			account.setError("");
		} catch (DeadlineExceededException e) {
			log.warning("获取最新微博超时:" + e.getMessage());
			return false;
		} catch (NetworkException e) {
			log.warning("netword error:"+e.getMessage()+",retry latter.");
			this.retryLatter(headers,params, YaYaConstants.ONE_MINUTE);
			return true;
		}catch(ApiReachLimitException e){
			log.warning(e.getMessage()+",suspend account,and retry latter.");
			account.suspendTo(new Date(System.currentTimeMillis()+YaYaConstants.TEN_MINUTE));
			account.save();
			this.retryLatter(headers,params, YaYaConstants.TEN_MINUTE);
			return true;
		}
		catch (YayaException e) {
			log.log(Level.SEVERE,
					"获取最新微博失败。用户:" + account.getUser().getEmail() + ",account:"
							+ account.getKeyName() + ", token:"
							+ account.getToken() + ",tokenSecret:"
							+ account.getTokenSecret());
			log.log(Level.SEVERE, e.getMessage(), e);
			account.setError(e.getMessage());
			return false;
		} finally {
			if (statuses != null && !statuses.isEmpty()) {
				account.increaseSyncFromCount(statuses.size());
				account.setLastSyncStatusId(statuses.get(0).getOriginalId());
				account.setSynced(false);
				user.setSynced(false);
				log.info("sync user:" + user.getEmail() + ", account:"
						+ account.getKeyName() + " total:" + statuses.size()
						+ " statuses");
			}
			account.setLastSyncDate(new Date());
			account.save();
			user.save();
		}
		return true;
	}

}
